﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Text;

using GeoSyndication.KML;
using GeoSyndication.GeoRSS;

namespace Examples
{
    // NOTE: If you change the class name "Feed1" here, you must also update the reference to "Feed1" in App.config.
    public class Feed1 : IFeed1
    {
        public SyndicationFeedFormatter CreateFeed()
        {
            // Create a new Syndication Feed.
            SyndicationFeed feed = new SyndicationFeed("Feed Title", "A WCF Syndication Feed", null);
            List<SyndicationItem> items = new List<SyndicationItem>();

            // Return ATOM or RSS based on query string
            // rss -> http://localhost:8731/Design_Time_Addresses/Examples/Feed1/
            // atom -> http://localhost:8731/Design_Time_Addresses/Examples/Feed1/?format=atom
            // kml -> http://localhost:8731/Design_Time_Addresses/Examples/Feed1/?format=kml
            string query = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
            SyndicationFeedFormatter formatter = null;
            if (query == "atom")
            {
                // Create a new Syndication Item.
                SyndicationItem item = new SyndicationItem("An item", "Item content", null);
                items.Add(item);
                feed.Items = items;

                formatter = new Atom10FeedFormatter(feed);
            }
            else if (query == "kml")
            {
                // Create a new Syndication Item.
                KMLData kmlData = new KMLData("Feed Title", "Feed Description");
                kmlData.AddLine("Line Item Title", "Line Item Description", "http://www.google.com", DateTime.Now, "", "49.211648 -124.037109 49.212102 -124.036987 49.212267 -124.036810 49.212358 -124.036579 49.212379 -124.035845 49.212959 -124.035536 49.213056 -124.035840 49.213167 -124.037486");

                feed = KMLFeedBuilder.GetFeed(kmlData);
                formatter = new KML21FeedFormatter(feed);
            }
            else
            {
                // Create a new Syndication Item.
                GeoRSSData geoData = new GeoRSSData("Feed Title", "Feed Description");
                geoData.AddLine("Line Item Title", "Line Item Description", "http://www.google.com", DateTime.Now, "", "49.211648 -124.037109 49.212102 -124.036987 49.212267 -124.036810 49.212358 -124.036579 49.212379 -124.035845 49.212959 -124.035536 49.213056 -124.035840 49.213167 -124.037486");

                feed = GeoRSSFeedBuilder.GetFeed(geoData);
                formatter = new Rss20FeedFormatter(feed);
            }

            return formatter;
        }
    }
}
